import tkinter as tk
from tkinter import ttk
import json
from src.types_pydantic.config_types import RootConfig
from src.globals import g_ic_debugger
from src.hardware.uds.zuds import Zuds_Tester_Base
from src.hardware.msgRead_thread import Backend_msgReader
from src.main_window.mainWindow import MainWindow
import threading
import time

from src.utils.msg_queue import global_can_unitTestRecvQueues
# 建立appname和运行程序的映射


def base(rooter):
    print("base")


def bggc_C62(rooter):
    g_ic_debugger("bggc_C62")
    from src.apps.common import Bggc_Seat_TestView
    Bggc_Seat_TestView(rooter=rooter)


def batc_B60(rooter):
    g_ic_debugger('batc_B60')
    from src.apps.common import Batc_TestView
    Batc_TestView(rooter=rooter)


def peu_c53fb(rooter):
    print("peu_c53fb")


app_map = {
    "base": base,
    "bggc_C62": bggc_C62,
    "batc_B60": batc_B60,
    "peu_c53fb": peu_c53fb,
}


class Rooter:
    def __init__(self):
        self.rootView = tk.Tk()
        self.rootView.title("控制器自动检测")
        # self.root.geometry("1000x800")
        self.rootView.protocol("WM_DELETE_WINDOW", self.on_close)

        self.config_init()

        self.view_init()

        # app_init 要在view_init之后，因为view_init中会用到app_init中的变量
        self.app_init()

        self.backend_msgRead_thread_init()

    def config_init(self):
        with open("config/app_config.json", "r", encoding='utf-8') as f:
            data = json.load(f)
            # g_ic_debugger(data)
            self.root_config: RootConfig = RootConfig(**data)
            g_ic_debugger(self.root_config)

    def view_init(self):
        self.view = tk.LabelFrame(self.rootView)
        self.view.pack()

    def on_close(self):
        self.rootView.destroy()
        print("Window closed")
        # exit()

    def run(self):
        self.rootView.mainloop()

    def app_init(self):

        # app_dict = self.root_config.apps[self.chengxuxuanze_Cmb.get()]
        # self.app_name = app_dict['name']
        self.app_name = self.root_config.app_select
        # g_ic_debugger(app_dict)
        self.ZUDS_TESTER = Zuds_Tester_Base(app_name=self.app_name)
        g_ic_debugger(self.ZUDS_TESTER)

        self.ZCANLIB = self.ZUDS_TESTER.zcanlib
        self.DEVICE_HANDLE = self.ZUDS_TESTER.device_handle
        self.CHN_HANDLE = self.ZUDS_TESTER.chn_handle
        self.GLOBAL_MSG_QUEUER = global_can_unitTestRecvQueues

        from src.hardware.uds_serve_lib import Serve_Func
        self.UDS_SERVE_FUNC = Serve_Func(tester=self.ZUDS_TESTER, rooter=self)

    def app_start(self):
        app_select = app_map[self.app_name]

        # 关闭程序选择窗口
        self.rootView.withdraw()

        # 显示app_select窗口
        # 把mainWindow的实例挂在ROOTER身上，后面好找到
        # FIXME: W开头的变量名，代表窗口，后面的变量名，代表实例
        self.W_mainWindow = MainWindow(ROOTER=self)

        app_select(self)

    def backend_msgRead_thread_init(self):
        self.BACKEND_MSGREADER = Backend_msgReader(
            zcanlib=self.ZCANLIB, chn_handle=self.CHN_HANDLE, global_recv_queues=self.GLOBAL_MSG_QUEUER)
        # 日常关闭接收线程，因为can报文接收和uds报文接收会冲突，所以在有需要时，再打开接收线程
        # self.BACKEND_MSGREADER.can_readMsg_thread_open()


def main():
    # print("Hello controller test ...")
    root = Rooter()
    # 在启动mainloop前，启动app_start函数，避免mainloop启动后，阻塞导致app_select函数无法启动

    def start_app():
        time.sleep(1)
        root.app_start()
    t = threading.Thread(target=start_app)
    t.start()

    root.run()

    pass


if __name__ == '__main__':
    if 0:
        from src.hardware.uds.zuds import test_Zuds_Tester_Base

        test_Zuds_Tester_Base()

        pass
    else:
        main()
